<?php

namespace App\Http\Service\web;

use App\Models\SystemStore;
use App\Models\SystemStoreGroup;
use Illuminate\Support\Facades\Request;

//分组
//ylb
class SystemGroupService
{

    /**
     * 修改
     * @param $id
     * @param Request $request
     * @return mixed
     * @throws \Exception
     */
    public static function updateService($id,$data)
    {

        $result = SystemStoreGroup::updateModel($id, $data);
        if (!$result) {
            throw  new \Exception('修改失败');
        }
        return  $result;
    }

    /**
     * 删除
     * @param $id
     * @return int
     * @throws \Exception
     */
    public static function groupDelService($id)
    {
        $data = SystemStore::where('group_id', $id)->get()->toArray();

        if (!empty($data)) {
            throw  new \Exception('分组下有数据，咱不能删除');
        }
        $res = SystemStoreGroup::groupDelModel($id);
        if (!$res) {
            throw  new  \Exception('删除失败');
        }
        return $res;

    }

    /**
     * 添加分组
     * @param $name
     * @param $status
     * @param $add_time
     * @return mixed
     * @throws \Exception
     */
    public static function addGroup($name, $status, $add_time)
    {

        if (empty($name)) throw  new  \Exception('分组名不能为空');
        if (empty($status)) throw  new  \Exception('分组状态不能为空');
        if (empty($add_time)) throw  new  \Exception('分组添加时间不能为空');
        $data = [
            'name' => $name,
            'status' => $status,
            'add_time' => $add_time,
        ];
        $res = SystemStoreGroup::addGroup($data);
        if (!$res) {
            throw new \Exception('添加失败');
        }
        return $res;

    }

    /**
     * 分组查询
     * @return mixed
     * @throws \Exception
     */
    public static function groupList()
    {
        $name = request()->get('name');
        $where = [];
        if (!empty($name)) {
            $where[] = ['id', 'like', "%$name%"];
        }
        $data = SystemStoreGroup::groupList($where);
        if (!$data) {
            throw  new  \Exception('查询失败');
        }
        return $data;
    }
}
